﻿using System.Collections.Generic;
using System.Linq;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using EPiServer.Core;
using EPiServer.Web.PropertyControls;
using TemplateFoundation.Core;
using TemplateFoundation.Exceptions;
using TemplateFoundation.PageTypes;

namespace TemplateFoundation.WebControls.PropertyControls
{
    /// <summary>
    /// Used to select a contact using a dropdown control
    /// </summary>
    public class PropertyContactControl : PropertyPageReferenceControl
    {
        private HtmlSelect _editControl;

        /// <summary>
        /// Creates a default PropertyContactControl control
        /// </summary>
        public PropertyContactControl()
        {
            Type = PropertyContactControlType.Any;
        }

        /// <summary>
        /// Creates a PropertyContactControl control for the specified type of contacts
        /// </summary>
        public PropertyContactControl(PropertyContactControlType type)
        {
            Type = type;
        }

        /// <summary>
        /// Applies the edit changes.
        /// </summary>
        public override void ApplyEditChanges()
        {
            if(_editControl==null || string.IsNullOrEmpty(_editControl.Value) || _editControl.SelectedIndex==0)
            {
                SetValue(PageReference.EmptyReference);
                return;
            }

            SetValue(new PageReference(int.Parse(_editControl.Value)));
        }

        /// <summary>
        /// Creates the edit controls.
        /// </summary>
        public override void CreateEditControls()
        {
            try
            {
                _editControl = new HtmlSelect();

                IEnumerable<ContactPageBase> pages;

                switch (Type)
                {
                    case PropertyContactControlType.Person:
                        pages = PageFactory.Instance.GetPersons(false).OfType<ContactPageBase>();
                        _editControl.Items.Add(LanguageManager.Instance.Translate("/properties/propertytags/selectcontactperson"));
                        break;
                    default:
                        pages = PageFactory.Instance.GetContacts(false).OfType<ContactPageBase>();
                        _editControl.Items.Add(LanguageManager.Instance.Translate("/properties/propertytags/selectcontact"));
                        break;
                }
                
                var selectedContactPerson = PropertyData.Value as PageReference;

                if (PageReference.IsNullOrEmpty(selectedContactPerson))
                {
                    selectedContactPerson = PageReference.EmptyReference;
                }

                foreach (var person in pages)
                {
                    var name = person.PageName;

                    if (person.Name!=null && !string.IsNullOrEmpty(person.Name.Trim()))
                    {
                        // Use Name property instead of page name if it has been specified
                        name = person.Name.Trim();
                    }

                    _editControl.Items.Add(new ListItem(name, person.PageLink.ID.ToString()));

                    if (person.PageLink==selectedContactPerson)
                    {
                        _editControl.SelectedIndex = _editControl.Items.Count - 1;
                    }
                }

                Controls.Add(_editControl);

            }
            catch (TemplateFoundationException)
            {
                var disabledDropDown = new HtmlSelect();
                disabledDropDown.Items.Add(LanguageManager.Instance.Translate("/properties/propertytags/unabletoloadcontacts"));
                disabledDropDown.Attributes.Add("disabled","disabled");
                Controls.Add(disabledDropDown);
            }
        }

        /// <summary>
        /// Gets the type of contact control, determining what kind of contacts the user can choose from
        /// </summary>
        public PropertyContactControlType Type
        {
            get; protected set;
        }
    }
}
